import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { VideoInfoDocumentType } from '../schema/video';
import uploadImage from '../utils/TencentCos';
@Injectable()
export class VideoService {
  constructor(
    @InjectModel('Video_Info')
    private readonly videoModel: Model<VideoInfoDocumentType>,
  ) {}

  //上传视频
  async upload(file, body) {
    const { description, url } = body;

    let local = false;
    let video_url = '';

    if (url) {
      local = true;
      video_url = url;
    } else {
      video_url = await uploadImage(file.buffer, file.mimetype, 'video/');
    }

    const video = new this.videoModel({
      description,
      local,
      video_url,
    });
    return await video.save();
  }

  //获取视频列表
  async list(query: any) {
    const { id } = query;

    if (id) {
      const result = await this.videoModel.findById(id);
      return {
        code: 200,
        data: result,
      };
    } else {
      const result = await this.videoModel.find();
      return {
        code: 200,
        data: result,
      };
    }
  }
}
